package com.hongyun.tms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.dto.CheckDTO;
import com.hongyun.tms.common.dto.dict.DictItemResponseDTO;
import com.hongyun.tms.common.dto.dict.MDictDataDTO;
import com.hongyun.tms.common.dto.dict.MDictTypeDTO;
import com.hongyun.tms.common.dto.dict.MDictTypeSearchDTO;
import com.hongyun.tms.common.page.PageParameter;
import com.hongyun.tms.common.po.MDictDataPO;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.utils.StringUtil;
import com.hongyun.tms.exception.BusinessRuntimeException;
import com.hongyun.tms.mapper.CommonMapper;
import com.hongyun.tms.mapper.MDictMapper;
import com.hongyun.tms.service.CommonService;
import com.hongyun.tms.service.MDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 字典及字典类型操作实现类
 *
 * @author 孙琦
 * @date 2023-4-26 16:57:35
 */
@Service
@Slf4j
public class MDictServiceImpl implements MDictService {


    @Resource
    private MDictMapper dictMapper;

    @Resource
    private CommonMapper commonMapper;

    @Autowired
    private CommonService commonService;

    /**
     * 查询字典类型列表
     *
     * @param mDictTypeSearchDTO  字典实体类
     * @return 响应数据
     */
    @Override
    public PageResponseDto<MDictTypeDTO> getDictTypeList(MDictTypeSearchDTO mDictTypeSearchDTO) {
        // 设置分页参数
        Page<MDictTypeDTO> page = new Page<>(mDictTypeSearchDTO.getStartPage(), mDictTypeSearchDTO.getPageSize());

        // 调用分页查询方法
        Page<MDictTypeDTO> resultPage = dictMapper.getDictTypeList(page, mDictTypeSearchDTO);

        // 构建分页响应对象
        PageResponseDto<MDictTypeDTO> voPage = new PageResponseDto<>(resultPage, MDictTypeDTO.class);
        return voPage;
    }


    /**
     * 新增、修改字典类型
     * @param po
     */
    @Override
    public void insertOrUpdateDictType(MDictTypeDTO po) {
        final String methodName = "DictServiceImpl:insertDictType";

        commonService.isRepeate("TMS_M_DICT_TYPE", "DICT_TYPE", po.getDictType(), StringUtil.getString(po.getId()), "字典类型编号", null);

        commonService.isRepeate("TMS_M_DICT_TYPE", "DICT_NAME", po.getDictName(), StringUtil.getString(po.getId()), "字典类型名称", null);

        if(po.getId()==null){
            po.setId(IdWorker.getId());
            dictMapper.insertDictType(po);

        }else{
            // 将该字典类型下的字典数据更改绑定
            dictMapper.changeDictType(po);
            dictMapper.updateDictType(po);

        }
    }

    /**
     * 根据id删除字典类型
     * @param id
     */
    @Override
    public void deleteDictTypeById(Long id) {

        final String methodName = "DictServiceImpl:deleteDictTypeById";

        MDictTypeDTO dict = dictMapper.getDictTypeById(id);

        Integer count = commonMapper.getCount("TMS_M_DICT_DATA", "DICT_TYPE", dict.getDictType());

        if(count!=null && count>0){

            throw new BusinessRuntimeException("该字典类型包含子数据不能删除！");

        }else{

            count = dictMapper.deleteDictTypeById(id);
            if(count<=0){
                throw new BusinessRuntimeException("字典类型删除失败！");
            }
        }

    }

    /**
     * 根据id查询字典类型
     *
     * @param id
     * @return
     */
    @Override
    public MDictTypeDTO getDictTypeById(Long id) {
        return dictMapper.getDictTypeById(id);
    }

    /**
     * 根据type返回字典项列表
     *
     * @param dictType type
     * @return list
     */
    @Override
    public List<DictItemResponseDTO> dictItemListByType(String dictType) {
        List<MDictDataDTO> list = dictMapper.getDictsByType(dictType);
        return MapperingUtils.copyList(list, DictItemResponseDTO.class);
    }
    //字典类型操作↑
            //字典操作↓

    /**
     * 根据id查询字典
     *
     * @param id
     * @return
     */
    @Override
    public MDictDataDTO getDictById(Long id) {
        final String methodName = "DictServiceImpl:getDictById";

        MDictDataDTO dict = dictMapper.getDictById(id);

        return dict;
    }



    /**
     * 新增、修改字典
     * @param po
     */
    @Override
    public void insertOrUpdateDict(MDictDataDTO po) {
        final String methodName = "DictServiceImpl:insertDict";

        // 其他条件
        List<CheckDTO> keyValues = new ArrayList<>();
        keyValues.add(CheckDTO.buildDTO("DICT_TYPE", po.getDictType()));
        commonService.isRepeate("TMS_M_DICT_DATA", "DICT_VALUE", po.getDictValue(), StringUtil.getString(po.getId()), "字典编号", keyValues);
        commonService.isRepeate("TMS_M_DICT_DATA", "DICT_LABEL", po.getDictLabel(), StringUtil.getString(po.getId()), "字典名称", keyValues);

        if(po.getId()==null){
            po.setId(IdWorker.getId());
            dictMapper.insertDict(po);
        }else{
            dictMapper.updateDict(po);
        }

    }


    /**
     * 根据字典类型获取字典
     * @param pageParameter
     * @param typeCd
     * @return
     */
    @Override
    public PageResponseDto<MDictDataDTO> getDictListByType(PageParameter pageParameter, String typeCd) {

        Page<MDictDataDTO> page = dictMapper.getDictListByType(typeCd);
        PageResponseDto<MDictDataDTO> voPage = new PageResponseDto<>(page, MDictDataDTO.class);
        return voPage;
    }


    /**
     * 删除字典
     * @param id
     */
    @Override
    public void deleteDictById(Long id) {
        Integer count = dictMapper.deleteDictById(id);
        if(count<=0){
                throw new BusinessRuntimeException("字典删除失败！");
            }

    }


    /**
     * 查询全部字典列表
     * @param pageParameter
     * @param po
     * @return
     */
    @Override
    public PageResponseDto<MDictDataDTO> getDictList(PageParameter pageParameter, MDictDataDTO po) {
//
        Page<MDictTypeDTO> page = new Page<>(pageParameter.getStartPage(), pageParameter.getPageSize());

        // 调用分页查询方法
        Page<MDictDataDTO> resultPage = dictMapper.getDictList(page, po);

        // 构建分页响应对象
        PageResponseDto<MDictDataDTO> voPage = new PageResponseDto<>(resultPage, MDictDataDTO.class);
        return voPage;
    }

    /**
     * 根据type和value的值返回对应的文本
     *
     * @param typeCode  DictType
     * @param dictValue DictValue
     * @return label
     */
    @Override
    public String getDictLabel(String typeCode, String dictValue) {
        MDictDataPO dictData =dictMapper.selectOneByTypeAndValue(typeCode, dictValue);
        return dictData != null ? dictData.getDictLabel() : null;
    }
}
